/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJFrame.java
 *
 * Created on 27-ene-2011, 3:49:10
 */

package GUI;

import Lexico.AnalizadorLexico;
import Lexico.Token;
import Sintactico.AnalizadorSintactico;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.String;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import maquinaV.MaquinaP;

/**
 *
 * @author Javier
 */
public class Interfaz extends javax.swing.JFrame {

    FileReader fr;
    FileReader fr2;
    BufferedReader bf,bufferP;
    ArrayList<String> lineaCP;
    private String sCadena;
    AnalizadorLexico analizador;
    AnalizadorSintactico analizador2;
    MaquinaP maquina;
    boolean modoTraza=false;
    ArrayList tokens;
    char[] buffer;

    String erroresLexico="";
    String erroresSintactico="";
    /** Creates new form NewJFrame */
    public Interfaz() {
        lineaCP=new ArrayList<String>();
        initComponents();

        /*try {
            fr = new FileReader("codigop.txt");
            bf= new BufferedReader(fr);
           fr2=new FileReader("C:/Users/Enrique/Documents/Facultad/PLG/codigop.txt");
           bufferP =new BufferedReader(fr2);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Interfaz.class.getName()).log(Level.SEVERE, null, ex);
        }*/
        BotonEjecutarTraza.setVisible(false);
        BotonEmpezar.setEnabled(false);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaCF = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaTokens = new javax.swing.JTextArea();
        BotonCompilar = new javax.swing.JButton();
        BotonCodigoP = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextAreaCP = new javax.swing.JTextArea();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        BotonEjecutarNormal = new javax.swing.JButton();
        BotonEmpezar = new javax.swing.JButton();
        BotonEjecutarTraza = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTextAreaPila = new javax.swing.JTextArea();
        jScrollPane5 = new javax.swing.JScrollPane();
        jTextAreaMD = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTabbedPane2 = new javax.swing.JTabbedPane();
        jScrollPane6 = new javax.swing.JScrollPane();
        jTextAreaEjec = new javax.swing.JTextArea();
        jScrollPane7 = new javax.swing.JScrollPane();
        jTextAreaSal = new javax.swing.JTextArea();
        jScrollPane8 = new javax.swing.JScrollPane();
        jTextAreaErr = new javax.swing.JTextArea();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextAreaCF.setColumns(20);
        jTextAreaCF.setRows(5);
        jScrollPane1.setViewportView(jTextAreaCF);

        jTextAreaTokens.setColumns(20);
        jTextAreaTokens.setRows(5);
        jScrollPane2.setViewportView(jTextAreaTokens);

        BotonCompilar.setText("Compilar");
        BotonCompilar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonCompilarActionPerformed(evt);
            }
        });

        BotonCodigoP.setText("CodigoP");
        BotonCodigoP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonCodigoPActionPerformed(evt);
            }
        });

        jLabel1.setText("Codigo Fuente");

        jLabel2.setText("Tokens Generados");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(110, 110, 110)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(57, 57, 57)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(BotonCompilar)
                    .addComponent(BotonCodigoP))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 85, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 296, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(102, 102, 102))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(203, 203, 203)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 428, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addGap(201, 201, 201))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane2)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 428, Short.MAX_VALUE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(182, 182, 182)
                        .addComponent(BotonCompilar)
                        .addGap(101, 101, 101)
                        .addComponent(BotonCodigoP)))
                .addContainerGap(72, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Traductor", jPanel2);

        jTextAreaCP.setColumns(20);
        jTextAreaCP.setRows(5);
        jScrollPane3.setViewportView(jTextAreaCP);

        jRadioButton1.setSelected(true);
        jRadioButton1.setText("Modo Normal");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        jRadioButton2.setText("Modo Traza");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        BotonEjecutarNormal.setText("Ejecutar");
        BotonEjecutarNormal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonEjecutarNormalActionPerformed(evt);
            }
        });

        BotonEmpezar.setText("Empezar");
        BotonEmpezar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonEmpezarActionPerformed(evt);
            }
        });

        BotonEjecutarTraza.setText("Ejecutar");
        BotonEjecutarTraza.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonEjecutarTrazaActionPerformed(evt);
            }
        });

        jTextAreaPila.setColumns(20);
        jTextAreaPila.setRows(5);
        jScrollPane4.setViewportView(jTextAreaPila);

        jTextAreaMD.setColumns(20);
        jTextAreaMD.setRows(5);
        jScrollPane5.setViewportView(jTextAreaMD);

        jLabel3.setText("Codigo Pila");

        jLabel4.setText("Cima--->");

        jLabel5.setText("Pila");

        jLabel6.setText("Memoria de Datos");

        jTextAreaEjec.setColumns(20);
        jTextAreaEjec.setRows(5);
        jScrollPane6.setViewportView(jTextAreaEjec);

        jTabbedPane2.addTab("Ejecucion", jScrollPane6);

        jTextAreaSal.setColumns(20);
        jTextAreaSal.setRows(5);
        jScrollPane7.setViewportView(jTextAreaSal);

        jTabbedPane2.addTab("Salida", jScrollPane7);

        jTextAreaErr.setColumns(20);
        jTextAreaErr.setRows(5);
        jScrollPane8.setViewportView(jTextAreaErr);

        jTabbedPane2.addTab("Errores", jScrollPane8);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(154, 154, 154)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 456, Short.MAX_VALUE)
                        .addComponent(jLabel5)
                        .addGap(310, 310, 310))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(64, 64, 64)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTabbedPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 865, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 79, Short.MAX_VALUE)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                                        .addComponent(jLabel4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(BotonEjecutarNormal)
                                            .addComponent(jRadioButton1)
                                            .addComponent(jRadioButton2)
                                            .addComponent(BotonEmpezar)
                                            .addComponent(BotonEjecutarTraza))
                                        .addGap(70, 70, 70)))
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(jPanel3Layout.createSequentialGroup()
                                        .addComponent(jLabel6)
                                        .addGap(166, 166, 166))
                                    .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 407, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 407, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(59, 59, 59)))
                .addGap(18, 18, 18))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(7, 7, 7)
                                .addComponent(jRadioButton1)
                                .addGap(18, 18, 18)
                                .addComponent(BotonEjecutarNormal))
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jRadioButton2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(BotonEmpezar)
                                .addGap(18, 18, 18)
                                .addComponent(BotonEjecutarTraza))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 317, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34))
        );

        jTabbedPane1.addTab("Interprete", jPanel3);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 996, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 594, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jMenu1.setText("File");

        jMenuItem1.setText("Cargar");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
        jRadioButton2.setSelected(true);
        jRadioButton1.setSelected(false);
        this.modoTraza=true;
        BotonEjecutarNormal.setEnabled(false);
        BotonEjecutarTraza.setEnabled(true);
        BotonEmpezar.setEnabled(true);
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    private void BotonEmpezarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonEmpezarActionPerformed

        jTabbedPane2.setSelectedIndex(0);
        jTextAreaEjec.setText("");
        jTextAreaErr.setText("");
        jTextAreaSal.setText("");
        jTextAreaPila.setText("");
        jTextAreaMD.setText("");
        BotonEjecutarTraza.setVisible(true);
     try {
           fr = new FileReader("codigop.txt");
           bf= new BufferedReader(fr);
           String s;
           
                lineaCP.add(bf.readLine());
                
                jTextAreaEjec.setText("Primera instruccion: "+lineaCP.get(0)+"\n");
       
          
               ArrayList al=new ArrayList();
               al=analizador2.getVariables();
                maquina=new MaquinaP(al,modoTraza);
        

        } catch (IOException ex) {
            Logger.getLogger(Interfaz.class.getName()).log(Level.SEVERE, null, ex);
        }
       
       
    }//GEN-LAST:event_BotonEmpezarActionPerformed

    private void BotonCompilarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonCompilarActionPerformed
        jTextAreaPila.setText("");
        jTextAreaMD.setText("");
        jTextAreaSal.setText("");
        jTextAreaEjec.setText("");
        if(analizador!=null) analizador=null;
        if(analizador2!=null) analizador2=null;
        String s=jTextAreaCF.getText();
        jTextAreaTokens.setText("");
        buffer=new char[s.length()+1];
            int i;
	         for(i=0;i<s.length();i++){
	          buffer[i]=s.charAt(i);
	         }
	         buffer[s.length()]='\f';
             analizador= new AnalizadorLexico(buffer);
             tokens= new ArrayList();

         tokens=analizador.sigToken();

         erroresLexico=analizador.getErrores();
	     for(int j=0;j<tokens.size();j++){
	       	Token token=(Token)tokens.get(j);
            jTextAreaTokens.append(token.toString()+"\n");
	     }
    }//GEN-LAST:event_BotonCompilarActionPerformed

    private void BotonCodigoPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonCodigoPActionPerformed
        analizador2=new AnalizadorSintactico(tokens);
        ArrayList codigo=analizador2.Inicio();
        erroresSintactico=analizador2.getErrores();
        jTextAreaErr.setText("ERRORES LEXICOS:\n");
        jTextAreaErr.setText(erroresLexico);
        jTextAreaErr.append("ERRORES SINTACTICOS:\n");
        jTextAreaErr.append(erroresSintactico);
        String saux="";
        jTextAreaPila.setText("");
        jTextAreaMD.setText("");
        for(int i=0;i<codigo.size();i++){
            saux+=codigo.get(i).toString();
        }
        FileWriter fw;
            try {
                File fichero=new File("codigop.txt");
                fw = new FileWriter("codigop.txt");
                fr = new FileReader("codigop.txt");
                fr2 = new FileReader("codigop.txt");
                bf= new BufferedReader(fr);
                fw.write(saux);
                fw.close();
            }
            catch (IOException ex) {
                Logger.getLogger(Interfaz.class.getName()).log(Level.SEVERE, null, ex);
            }


        jTabbedPane1.setSelectedIndex(1);
        String s="";
        String cad="";
        int cont=0;
        try {
        	 fr2=new FileReader("codigop.txt");
             bufferP =new BufferedReader(fr2);
             while ((s=bufferP.readLine()) != null) {
                if(cont<10) cad=cad+" "+String.valueOf(cont)+".  "+s+"\n";
                else cad=cad+String.valueOf(cont)+". "+s+"\n";
                cont++;
            }
            fr2.close();
        } catch (IOException ex) {
            Logger.getLogger(Interfaz.class.getName()).log(Level.SEVERE, null, ex);
        }
        jTextAreaCP.setText(cad);
        if (analizador2.dameError()) {
           BotonEjecutarNormal.setEnabled(false);
           BotonEmpezar.setEnabled(false);
           BotonEjecutarTraza.setEnabled(false);
           jTabbedPane2.setSelectedIndex(2);
        }else{
              BotonEjecutarNormal.setVisible(true);
              BotonEmpezar.setEnabled(true);
              BotonEjecutarTraza.setEnabled(true);
        }
      
    }//GEN-LAST:event_BotonCodigoPActionPerformed

    private void BotonEjecutarNormalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonEjecutarNormalActionPerformed

    jTabbedPane2.setSelectedIndex(0);
    jTextAreaEjec.setText("");
    jTextAreaErr.setText("");
    jTextAreaSal.setText("");
        
    ArrayList al=new ArrayList();
       al=analizador2.getVariables();
       maquina=new MaquinaP(al,modoTraza);        
        maquina.ejecuta();
        String pila="Estado final de la pila: \n"+maquina.pilaToStrint();
        jTextAreaPila.setText(pila);
        String memoria="Estado final de la memoria: \n"+ maquina.memoriaToString();
        jTextAreaMD.setText(memoria);
        jTextAreaSal.setText(maquina.getOut());
        jTextAreaErr.setText(maquina.getErrores());
        if(maquina.getHayErrores()) jTabbedPane2.setSelectedIndex(2);
        else jTabbedPane2.setSelectedIndex(1);
    }//GEN-LAST:event_BotonEjecutarNormalActionPerformed


    private void BotonEjecutarTrazaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonEjecutarTrazaActionPerformed
         
        //!lineaCP.equals("\f")
        if(!maquina.getParada()){
            maquina.ejecuta();
        try {
             for(int i=0;i<maquina.numLineas();i++)
             lineaCP.add(bf.readLine());
        } catch (IOException ex) {
            Logger.getLogger(Interfaz.class.getName()).log(Level.SEVERE, null, ex);
        }
        String pila=maquina.pilaToStrint();
        jTextAreaPila.setText(pila);
        String memoria= maquina.memoriaToString();
        jTextAreaMD.setText(memoria);
        jTextAreaSal.setText(maquina.getOut());
        jTextAreaErr.setText(maquina.getErrores());
        if (maquina.getParada()==false)  jTextAreaEjec.append("Siguiente instruccion: "+lineaCP.get(maquina.getCProg())+"\n");
        if (maquina.getHayErrores()==true) jTabbedPane2.setSelectedIndex(2);
        }
        else jTabbedPane2.setSelectedIndex(1);
    }//GEN-LAST:event_BotonEjecutarTrazaActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
       JFileChooser chooser = new JFileChooser("./Pruebas");
          chooser.setApproveButtonText("Abrir TxT");
          chooser.showOpenDialog(null);
          File archivo=chooser.getSelectedFile();
         //char[] buffer=new char[12];
          try {
            sCadena="";
            jTextAreaCP.setText("");
            jTextAreaMD.setText("");
            BufferedReader reader = new BufferedReader(new FileReader(archivo));
            String linea = reader.readLine();
            while (linea != null) {
            for(int i=0;i<linea.length();i++){
                if((linea.charAt(i)==';')&&(i!=linea.length()-1)){
                    sCadena+=linea.charAt(i)+"\n";
                }
                else if (i==linea.length()-1) sCadena+=linea.charAt(i)+"\n";
                        else sCadena+=linea.charAt(i);
	         }
            linea = reader.readLine();
            }
            jTextAreaCF.setText(sCadena);
            /*buffer=new char[sCadena.length()+1];
            int i;
	         for(i=0;i<sCadena.length();i++){
	          buffer[i]=sCadena.charAt(i);
	         }
	          buffer[sCadena.length()]='\f';
            //  analizador= new AnalizadorLexico(buffer);*/
         }
          catch (Exception ex) {
             System.out.println("Error al abrir el archivo");
          }
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        jRadioButton1.setSelected(true);
        jRadioButton2.setSelected(false);
        modoTraza=false;
        BotonEjecutarTraza.setEnabled(false);
        BotonEmpezar.setEnabled(false);
        BotonEjecutarNormal.setEnabled(true);
    }//GEN-LAST:event_jRadioButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Interfaz().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BotonCodigoP;
    private javax.swing.JButton BotonCompilar;
    private javax.swing.JButton BotonEjecutarNormal;
    private javax.swing.JButton BotonEjecutarTraza;
    private javax.swing.JButton BotonEmpezar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JScrollPane jScrollPane7;
    private javax.swing.JScrollPane jScrollPane8;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JTextArea jTextAreaCF;
    private javax.swing.JTextArea jTextAreaCP;
    private javax.swing.JTextArea jTextAreaEjec;
    private javax.swing.JTextArea jTextAreaErr;
    private javax.swing.JTextArea jTextAreaMD;
    private javax.swing.JTextArea jTextAreaPila;
    private javax.swing.JTextArea jTextAreaSal;
    private javax.swing.JTextArea jTextAreaTokens;
    // End of variables declaration//GEN-END:variables

}
